package com.gbiac.modules.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gbiac.modules.monitor.entity.Seat;
import com.gbiac.modules.monitor.entity.SeatUser;
import com.gbiac.modules.monitor.entity.UserCheckIn;
import com.gbiac.modules.monitor.mapper.SeatUserMapper;
import com.gbiac.modules.monitor.param.ForceCheckOutAllParam;
import com.gbiac.modules.monitor.param.ForceCheckOutParam;
import com.gbiac.modules.monitor.param.UserCheckParam;
import com.gbiac.modules.monitor.service.ISeatService;
import com.gbiac.modules.monitor.service.ISeatUserService;
import com.gbiac.modules.monitor.service.IUserCheckInService;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 席位当前用户
 * @Author: jeecg-boot
 * @Date:   2024-11-30
 * @Version: V1.0
 */
@Service
public class SeatUserServiceImpl extends ServiceImpl<SeatUserMapper, SeatUser> implements ISeatUserService {

    @Resource
    private IUserCheckInService userCheckInService;
    @Resource
    private ISeatService seatService;

    @Override
    public void setSeatUser(String seatId, String userId, Date checkInTime) {
        // 设置当前席位用户
        SeatUser seatUser = getOne(new LambdaQueryWrapper<SeatUser>().eq(SeatUser::getSeatId, seatId));
        if (seatUser == null) {
            seatUser = new SeatUser();
        }
        seatUser.setSeatId(seatId);
        seatUser.setUserId(userId);
        seatUser.setCheckInTime(checkInTime);

        saveOrUpdate(seatUser);
    }

    @Override
    public void clearSeatUser(String seatId) {
        // 清除当前席位用户
        SeatUser seatUser = getOne(new LambdaQueryWrapper<SeatUser>().eq(SeatUser::getSeatId, seatId));
        if (seatUser == null) {
            return;
        }
        update(Wrappers.<SeatUser>lambdaUpdate()
                .set(SeatUser::getUserId, null)
                .set(SeatUser::getCheckInTime, null)
                .eq(SeatUser::getId, seatUser.getId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void forceCheckOut(ForceCheckOutParam param) {
        Seat seat = seatService.getById(param.getSeatId());
        seat.setDisable("1");
        seatService.updateById(seat);

        SeatUser seatUser = getOne(new LambdaQueryWrapper<SeatUser>().eq(SeatUser::getSeatId, param.getSeatId()));
        if (seatUser == null) {
            return;
        }
        userCheckInService.checkOut(seatUser.getUserId(), seatUser.getSeatId());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void forceCheckOutAll(ForceCheckOutAllParam param) {
        // 强制离线所有席位
        List<Seat> seatList = seatService.list(new LambdaQueryWrapper<Seat>().eq(Seat::getOrgCode, param.getOrgCode()));
        if (CollectionUtils.isEmpty(seatList)) {
            return;
        }
        for (Seat seat : seatList) {
            seat.setDisable("1");
            seatService.updateById(seat);
            SeatUser seatUser = getOne(new LambdaQueryWrapper<SeatUser>().eq(SeatUser::getSeatId, seat.getId()));
            if (seatUser == null) {
                continue;
            }
            userCheckInService.checkOut(seatUser.getUserId(), seatUser.getSeatId());
        }

    }

    @Override
    public boolean isCheckOutTemp(String seatId) {
        SeatUser seatUser = getOne(new LambdaQueryWrapper<SeatUser>().eq(SeatUser::getSeatId, seatId));
        if (seatUser == null) {
            return false;
        }
        UserCheckIn userCheckIn = userCheckInService.getOne(new LambdaQueryWrapper<UserCheckIn>()
                .eq(UserCheckIn::getSeatId, seatUser.getSeatId())
                .eq(UserCheckIn::getUserId, seatUser.getUserId())
                        .orderByDesc(UserCheckIn::getCheckInTime)
                , false);
        if (userCheckIn == null) {
            return false;
        }
        if (userCheckIn.getCheckOutTime() == null) {
            return false;
        }
        return true;
    }
}
